package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.DoctorDao;
import cn.edu.cdtu.healthserver.domain.Doctor;
import cn.edu.cdtu.healthserver.domain.Register;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

public class DoctorDAOImp implements DoctorDao {

	
//	使用dbutil工具
	QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
	@Override
	public List<Doctor> query() {
		// TODO Auto-generated method stub
		String sql = "select * from doctor";
		
		try {
//			BeanListHandler<A>(B),A是被转换成的OBJECT类型，B是创建文件的类型来源
			return qr.query(sql, new BeanListHandler<Doctor>(Doctor.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
	}

	@Override
	public List<Doctor> query(int dep_id) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM doctor WHERE dep_id = '1'";
		try {
			return qr.query(sql, new BeanListHandler<Doctor>(Doctor.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
//			要抛出一个返回值
			throw new RuntimeException();
		}
		
	}
	
	public void addReg(Register reg){
		
		
		 /*String sql = 
				"INSERT INTO `doctor`.`register` (`doc_id`, `pat_id`, `register_date`) "
				+ "VALUES ('"+reg.getD_id()+"', '"+reg.getP_id()+"', '2015-03-21 10:53:56')";
	*/
		  String sql ="insert into register(doc_id,pat_id,register_date) values(?,?,?)";
		   Object[] params={reg.getD_id(),reg.getP_id(),reg.getDate()};
		   try {
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
